
#include "px_iic2.h"
#include "ax_iic2.h" 
#include "ax_sys.h"
#include "ax_delay.h"
#include <stdio.h> 
#define	MC5883l_ADDR    0x30   
uint8_t ax_iic2_code=0;
#define	check_IIC_addr   0
  
 
 
    void test_IIC_ADDR(){
        for(int i=0;i<0x7F;i++){
            uint8_t  code=	test(i);
            if(code==60){
                    printf("iic2 %X asked",i);
                    AX_Delayus(10000);  
            }else if(code==50){
                    //printf("iic2 %X no ask",i);
            }else{
                    printf("iic2 %X start error %d",i,code);
                    AX_Delayus(10000);  
            } 
        } 
    }

    uint8_t MMC5983_Enable( ) { 
        unsigned char reg_val = 0; 
        ax_iic2_code=1;
        while(ax_iic2_code!=0){ 
            ax_iic2_code=PX_IIC2_Read(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_STATUS, 1,&reg_val);	//Read reg 0x08, check OTP_Read_Done bit 
            if(ax_iic2_code>0){
                printf("iic2 read done error %d",ax_iic2_code);
                AX_Delayus(100000);	  
                continue;
            } 
            if((reg_val&MMC5983_OTP_READ_DONE) != MMC5983_OTP_READ_DONE){ 
                printf("iic2 MMC5983 Enable error %d",reg_val); 
            } 
        }
        ax_iic2_code=1;
        while(ax_iic2_code!=0){
            unsigned char pro_id = 0; 
            ax_iic2_code=PX_IIC2_Read(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_PRODUCTID1,1, &pro_id);	// Read reg 0x2F, check product ID, default value is 0x30 
            if(ax_iic2_code>0){
                    printf("iic2 read PRODUCTID error  %d",ax_iic2_code);
                    AX_Delayus(100000);	
                continue;
            }
            if(pro_id != MMC5983_PRODUCT_ID){ 
                printf("iic2 MMC5983 id error %d",pro_id);
            
            }
         
        }
           
        ax_iic2_code=1;
        while(ax_iic2_code!=0){
            ax_iic2_code=PX_IIC2_Write_byte(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL1,MMC5983_CMD_BW00 ); 
            if(ax_iic2_code>0){
                printf("iic2 Write 0X0A   %d",ax_iic2_code);
                AX_Delayus(100000);	
                continue;
             }
         }
        ax_iic2_code=1;
        while(ax_iic2_code!=0){
             ax_iic2_code=PX_IIC2_Write_byte(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL0,MMC5983_CMD_AUTO_SR_EN); 
             if(ax_iic2_code>0){
                    printf("iic2 Write 0X09   %d",ax_iic2_code);
                    AX_Delayus(100000);	
                 continue;
             }
         }
        ax_iic2_code=1;
        while(ax_iic2_code!=0){
            ax_iic2_code=PX_IIC2_Write_byte(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL2,MMC5983_CMD_CMM_EN|MMC5983_CMD_CM_FREQ_50HZ); 
             if(ax_iic2_code>0){
                    printf("iic2 Write 0X0B  %d",ax_iic2_code);
                    AX_Delayus(100000);	
                 continue;
             }
         }
             
        return 0;
    }
     void MMC5983_Disable(void){ 
        PX_IIC2_Write_byte(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL2,0x00); 
        return;
    }

    uint8_t AX_IIC2_Init(void){	 
        PX_IIC2_Init();  
        #if check_IIC_addr==1
            test_IIC_ADDR();
        #endif 
        return MMC5983_Enable(); 
    }
 
 uint8_t MMC5983_GetData(float *mag_out) {
    uint8_t data_reg[7] = {0};
    uint32_t data18bit[3] = {0};
    for(int i=0;i<7;i++){
        ax_iic2_code=1;
        while(ax_iic2_code!=0){
            ax_iic2_code= PX_IIC2_Read_byte(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_DATA+i,  &data_reg[i]);
             if(ax_iic2_code>0){
                 AX_Delayus(1000);
             }
        }
    } 
      /* Get 18bits data, raw data unit is "count or LSB" */  
  data18bit[0] = (uint32_t)(data_reg[0]<<10 | data_reg[1]<<2 | (data_reg[6]&0xC0)>>6);
  data18bit[1] = (uint32_t)(data_reg[2]<<10 | data_reg[3]<<2 | (data_reg[6]&0x30)>>4);
  data18bit[2] = (uint32_t)(data_reg[4]<<10 | data_reg[5]<<2 | (data_reg[6]&0x0C)>>2);	
        
  /* Magnetic field output, unit is Gauss */
  mag_out[0] = ((float)data18bit[0] - MMC5983_18BIT_OFFSET)/MMC5983_18BIT_SENSITIVITY; 
  mag_out[1] = ((float)data18bit[1] - MMC5983_18BIT_OFFSET)/MMC5983_18BIT_SENSITIVITY;
  mag_out[2] = ((float)data18bit[2] - MMC5983_18BIT_OFFSET)/MMC5983_18BIT_SENSITIVITY;
        
    return 0;
}






